﻿using Sidvall.UI.Uwp.Test.ViewModels;
using Sidvall.UI.Views;
using System;
using System.IO;
using System.Threading.Tasks;
using Windows.Devices.Geolocation;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls.Maps;
using Windows.UI.Xaml.Media.Imaging;

namespace Sidvall.UI.Uwp.Test.Views
{
    public sealed partial class Page2View : IViewModelProvider
    {
        #region Public Members

        public new Page2ViewModel ViewModel
        {
            get
            {
                return (Page2ViewModel)base.ViewModel;
            }
            set
            {
                base.ViewModel = value;
            }
        }

        Sidvall.UI.ViewModels.IViewModel IViewModelProvider.ViewModel
        {
            get
            {
                return this.ViewModel;
            }
        }

        #endregion
        #region Private Members

        #region CopyFileAsync

        private async Task CopyFileAsync(string path)
        {
            var folderPath = Sidvall.SystemContext.Current.SystemStorageManager.Combine(Sidvall.SystemContext.Current.SystemStorageManager.GetFolderPath(IO.FolderType.LocalApplicationData),
                "Projects\\1");
            if (!(await Sidvall.SystemContext.Current.SystemStorageManager.FolderExistsAsync(folderPath)))
            {
                await Sidvall.SystemContext.Current.SystemStorageManager.CreateFolderAsync(folderPath);
            }
            var filePath = Sidvall.SystemContext.Current.SystemStorageManager.Combine(folderPath, "1" + System.IO.Path.GetExtension(path));
            await Sidvall.SystemContext.Current.SystemStorageManager.CopyFileAsync(path, filePath, true);
        }

        #endregion
        #region ShowPictureAsync

        private async Task ShowPictureAsync(string path)
        {
            var img = new BitmapImage();
            using (var stream = await Sidvall.SystemContext.Current.SystemStorageManager.OpenFileAsync(path, IO.FileMode.Open, IO.FileAccess.Read))
            {
                await img.SetSourceAsync(stream.AsRandomAccessStream());
                mediaPreivew.Source = img;
            }
        }

        #endregion
        #region SetPoint

        private void SetPoint(double latitude, double longitude)
        {
            // Specify a known location.
            BasicGeoposition snPosition = new BasicGeoposition() { Latitude = latitude, Longitude = longitude };
            Geopoint snPoint = new Geopoint(snPosition);

            // Create a MapIcon.
            MapIcon mapIcon1 = new MapIcon();
            mapIcon1.Location = snPoint;
            mapIcon1.NormalizedAnchorPoint = new Point(0.5, 1.0);
            mapIcon1.Title = "Space Needle";
            mapIcon1.ZIndex = 0;

            // Add the MapIcon to the map.
            map.MapElements.Clear();
            map.MapElements.Add(mapIcon1);

            // Center the map over the POI.
            map.Center = snPoint;
            map.ZoomLevel = 14;

            this.txtLatitude.Text = latitude.ToString();
            this.txtLongitude.Text = longitude.ToString();
        }

        #endregion

        #endregion
        #region Events

        private async void button_Click(object sender, RoutedEventArgs e)
        {
            using (var busyManager = new Sidvall.Data.BusyManager(this.ViewModel))
            {
                try
                {
                    var photo = await Sidvall.SystemContext.Current.CameraManager.CapturePhotoAsync(false, Devices.PhotoFormat.Jpeg, Devices.PhotoResolution.HighestAvailable);
                    if (photo != null)
                    {
                        var prop = await photo.GetImagePropertiesAsync();
                        SetPoint(prop.Latitude.Value, prop.Longitude.Value);
                        await CopyFileAsync(photo.Path);
                        await ShowPictureAsync(photo.Path);
                    }
                }
                catch (System.Exception ex)
                {
                    await Sidvall.UI.SystemContext.Current.DialogManager.ShowErrorMessageAsync(ex);
                }
            }
        }
        private async void gpsButton_Click(object sender, RoutedEventArgs e)
        {
            using (var busyManager = new Sidvall.Data.BusyManager(this.ViewModel))
            {
                try
                {
                    var geolocator = await Sidvall.SystemContext.Current.GeolocatorFactoryManager.CreateGeolocatorAsync();
                    var pos = await geolocator.GetGeopositionAsync();
                    SetPoint(pos.Point.Latitude, pos.Point.Longitude);
                }
                catch (System.Exception ex)
                {
                    await Sidvall.UI.SystemContext.Current.DialogManager.ShowErrorMessageAsync(ex);
                }
            }
        }

        #endregion
        #region Constructors

        public Page2View()
        {
            this.InitializeComponent();
        }

        #endregion
    }
}
